#!/usr/bin/env python

from aligned_parser import *
import boto
from boto.exception import SDBResponseError


def readData(directory, data_files, file_format, sdb):
    naacl = {}
    giza = {}
    domains = set()
    dataset = set()

    all_domains = sdb.get_all_domains()

    for domain in all_domains:
        [name, domain_name] = str(domain).split(':')
        domains.add(domain_name)
    if not file_format in domains:
        domain = sdb.create_domain(file_format)
    else:
        domain = sdb.get_domain(file_format)
        
    for item in domain:
        dataset.add(frozenset(item['words']))
        
    c = len(dataset)
    print c

    print '------------------------------------------------------'

    aligned_sents_naacl = NaaclReader(directory, data_files)
    for sent in aligned_sents_naacl.sents:
        if not frozenset(sent.words) in dataset:
            print 'yay no same item exists'
            c += 1
            index = 'sent' + str(c)
            item = domain.new_item(index)
            
            item['words'] = sent.words
            item['mots'] = sent.mots
            item['align'] = sent.alignment
            if file_format == 'naacl':
                item['possible_align'] = sent.possible_alignment
            item.save()
        else:
            print 'item already exists'
        

def queryData():
    return


if  __name__ == '__main__':
    key_file = open('key.dat', 'r')
    access_key = key_file.readline().strip()
    private_key = key_file.readline().strip()
    key_file.close()
    
    sdb = boto.connect_sdb(access_key, private_key)
    print sdb.get_all_domains()

    
    directory = '../../../data/naacl/'
    data_file1 = 'trial.e'
    data_file2 = 'trial.f'
    align_file = 'trial.wa'
    data_files = [data_file1, data_file2, align_file]
    readData(directory, data_files, 'naacl', sdb)



##    sdb.delete_domain('naacl')
                                            
##    domains = sdb.get_all_domains()
##    print domains
##    for domain in domains:
##        [name, domain_name] = str(domain).split(':')
##        if 'test_domain' == domain_name:
##            print 'yes! domain exixts!'
##    
        

##item_1 = 'item1'
##same_value = 'same_value'
##attrs_1 = {'name1' : same_value, 'name2' : 'diff_value_1'}
##domain.put_attributes(item_1, attrs_1)

##print domain.get_attributes(item_1)
